from __future__ import annotations

import streamlit as st

from gssgca.transmission_line.cableway_selection import select_cableway
from gssgca.lifting.winch_selection import select_winch
from gssgca.lifting.block_detailed import reeving_analysis
from gssgca.transmission_line.multi_weather_sag import sag_under_multiple_weathers
from gssgca.foundation.slope_bishop import bishop_simplified, BishopSlice


st.set_page_config(page_title="GSSGCA 简易计算", layout="wide")
st.title("电力施工计算 - 简易页面")
st.caption("示例页面：覆盖常用计算接口，参数最小化。")

tabs = st.tabs(["索道选型", "绞磨选型", "滑车组", "多气象弧垂", "Bishop圆弧"]) 

with tabs[0]:
    st.subheader("索道主绳选型")
    c1, c2, c3 = st.columns(3)
    span = c1.number_input("跨距(m)", 300.0)
    payload = c2.number_input("吊重(kN)", 100.0)
    terrain = c3.selectbox("地形", ["plain", "hills", "mountain"], 2)
    if st.button("计算", key="cw"): 
        scheme = select_cableway(span, payload, terrain)
        st.json({
            "直径mm": scheme.main_rope_diameter_mm,
            "破断kN": scheme.main_rope_breaking_kN,
            "SF": round(scheme.safety_factor, 2),
            "建议小车kN": scheme.recommend_carriage_weight_kN,
            "备注": scheme.note,
        })

with tabs[1]:
    st.subheader("绞磨/卷扬机选型")
    c1, c2, c3 = st.columns(3)
    maxT = c1.number_input("最大张力(kN)", 120.0)
    eta = c2.number_input("效率η", 0.9)
    sf = c3.number_input("安全系数", 1.25)
    if st.button("计算", key="winch"):
        res = select_winch(maxT, eta, sf)
        st.json({"所需线拉kN": round(res.required_line_pull_kN,2), "推荐吨位t": round(res.recommended_winch_t,2)})

with tabs[2]:
    st.subheader("滑车组分析")
    c1, c2 = st.columns(2)
    hook = c1.number_input("吊钩荷载(kN)", 100.0)
    parts = c2.number_input("工作绳股数", 4, step=1)
    if st.button("计算", key="block"):
        res = reeving_analysis(hook, int(parts))
        st.json({"效率": round(res.efficiency,3), "跑绳拉力kN": round(res.running_rope_tension_kN,2), "死端kN": round(res.dead_end_tension_kN,2)})

with tabs[3]:
    st.subheader("多气象应力-弧垂")
    c1, c2, c3 = st.columns(3)
    span = c1.number_input("代表档距(m)", 420.0)
    w = c2.number_input("线重(kN/m)", 0.0048)
    H0 = c3.number_input("初张力(kN)", 20.0)
    if st.button("计算", key="sag"):
        pts = sag_under_multiple_weathers(
            span_m=span, unit_weight_kN_m=w, elastic_modulus_MPa=73000, area_mm2=300,
            alpha_per_C=1.9e-5, temp_ref_C=15, initial_tension_kN=H0, conductor_diameter_mm=21.6,
            weather_cases=[{"name":"ref"}]
        )
        st.table({"weather":[p.weather for p in pts], "tension(kN)": [p.tension_kN for p in pts], "sag(m)": [p.sag_m for p in pts]})

with tabs[4]:
    st.subheader("Bishop 圆弧滑动")
    st.caption("输入两条分块作为示例，可在代码中扩展")
    s1 = BishopSlice(width_m=1.0, height_m=5.0, unit_weight_kN_m3=18.0, cohesion_kPa=10.0, friction_deg=20.0, base_inclination_deg=20.0)
    s2 = BishopSlice(width_m=1.0, height_m=5.0, unit_weight_kN_m3=18.0, cohesion_kPa=10.0, friction_deg=20.0, base_inclination_deg=25.0)
    if st.button("计算", key="bishop"):
        res = bishop_simplified([s1, s2])
        st.json({"Fs": round(res.factor_of_safety,3), "iters": res.iterations, "converged": res.converged})


